<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix"><span>魔方</span></div>
    <div>
      <el-collapse v-model="activeNames">
        <el-collapse-item title="组件状态" name="1">
          <el-form size="medium" label-width="100px">
            <el-form-item label="类型">
              <el-radio-group size="mini" v-model="data.type" @change="changetype">
                <el-radio :label="0">一行两个</el-radio>
                <el-radio :label="1">一行三个</el-radio>
                <el-radio :label="2">左一右二</el-radio>
                <el-radio :label="3">上一下二</el-radio>
                <el-radio :label="4">左一右三</el-radio>
                <el-radio :label="5">自定义</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="魔方密度" v-if="data.type == 5">
              <el-radio-group size="mini" v-model="num">
                <el-radio :label="4">4x4</el-radio>
                <el-radio :label="5">5x5</el-radio>
                <el-radio :label="6">6x6</el-radio>
                <el-radio :label="7">7x7</el-radio>
                <!-- <el-radio label="4">4x4</el-radio> -->
              </el-radio-group>
            </el-form-item>
            <el-form-item label="魔方布局">
              <div class="custom_box" :style="'height:' + data.height + 'px'">
                <ul class="cube-items" v-for="(item, i) in num" v-if="data.type == 5">
                  <li @mouseover="overitem(i, z)" @click="clickitem(i, z)" class="cube-item" :class="getCalss(i, z)" v-for="(data, z) in num"></li>
                </ul>
                <div
                  class="cue-check-item "
                  :class="actvi_id == i ? 'active' : ''"
                  v-for="(item, i) in data.checklist"
                  @click="changeitem(i)"
                  :style="'left: ' + item.x + '%; top: ' + item.y + '%; width:' + item.width + '%; height: ' + item.height + '%;'"
                >
                  <div class="item-box">
                    <div class="cube-text">
                      <vimage v-if="item.img" :src="item.img"></vimage>
                      <div class="text" v-else>
                        <template v-if="data.type == 0 || data.type == 1">
                          <p>宽度{{ Math.ceil((item.width * 750) / 100) }}px</p>
                        </template>
                        <template v-else>
                          <template v-if="item.width > 20">
                            <p>
                              {{ Math.ceil((item.width * 750) / 100) }}x{{ Math.ceil((item.height * 750) / 100) }}
                              <span>像素</span>
                            </p>
                            <p>或同等比例</p>
                          </template>
                          <template v-else>
                            <p>{{ Math.ceil((item.width * 750) / 100) }}x{{ Math.ceil((item.height * 750) / 100) }}</p>
                          </template>
                        </template>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="选择图片" v-if="data.checklist[actvi_id]">
              <div class="item">
                <!-- <i class="icon-abl-close el-icon-error" @click="removePic(i)"></i> -->
                <!-- <div class="dragbox"><i class="el-icon-d-caret"></i></div> -->
                <div class="img_box"><Upload :tempUrl="img" @input="getimg"></Upload></div>
                <div class="item_select" >
                  <!-- <div class="select_link" style="margin-bottom:8px;">
                      <div class="select_btn" @click="selectlink">
                        <i class="el-icon-link"></i>
                        &nbsp;
                        <span>选择链接</span>
                      </div>
                    </div> -->
                  <div @click="selectlink()"><Linkdiv :linkdata="data.checklist[actvi_id].linkdata"></Linkdiv></div>

                  <span class="tip"></span>
                </div>
              </div>
            </el-form-item>
            <el-form-item label="内部间距">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.padding.between"></el-slider>
                <el-input-number v-model="data.padding.between" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
            <el-form-item label="上间距">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.padding.top"></el-slider>
                <el-input-number v-model="data.padding.top" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
            <el-form-item label="下间距">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.padding.bottom"></el-slider>
                <el-input-number v-model="data.padding.bottom" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
            <el-form-item label="左右间距">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.padding.lr"></el-slider>
                <el-input-number v-model="data.padding.lr" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
            <el-form-item label="上边角">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.r_t"></el-slider>
                <el-input-number v-model="data.r_t" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
            <el-form-item label="下边角">
              <div class="slider_box">
                <el-slider class="slider_item" :max="50" v-model="data.r_b"></el-slider>
                <el-input-number v-model="data.r_b" :controls="false" :min="0" :max="50"></el-input-number>
              </div>
            </el-form-item>
          </el-form>
        </el-collapse-item>
      </el-collapse>
    </div>
  </el-card>
</template>

<script>
import Linkdiv from '@/components/Link/index';

import Upload from '@/components/Upload/SingleImage2';
import {getImageInfo} from '@/utils/index';
export default {
  props: {
    data: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      selectlist: [],
      nextitem: null,
      sitem: null,
      activeNames: ['1'],
      num: 7,
      actvi_id: 0,
      img: '',
      type: 0,
      checkdefaultlist: [
        [
          {
            width: 50,
            height: 100,
            x: 0,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 100,
            x: 50,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          }
        ],
        [
          {
            width: (1 / 3) * 100,
            height: 100,
            x: 0,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: (1 / 3) * 100,
            height: 100,
            x: (1 / 3) * 100,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: (1 / 3) * 100,
            height: 100,
            x: (2 / 3) * 100,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          }
        ],
        [
          {
            width: 50,
            height: 100,
            x: 0,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 50,
            x: 50,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 50,
            x: 50,
            y: 50,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          }
        ],
        [
          {
            width: 100,
            height: 50,
            x: 0,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 50,
            x: 0,
            y: 50,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 50,
            x: 50,
            y: 50,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          }
        ],
        [
          {
            width: 50,
            height: 100,
            x: 0,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 50,
            height: 50,
            x: 50,
            y: 0,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 25,
            height: 50,
            x: 50,
            y: 50,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          },
          {
            width: 25,
            height: 50,
            x: 75,
            y: 50,
            img: '',
            linkdata: {
              link: '',
              name: '',
              id: '',
              type: ''
            }
          }
        ]
      ]
    };
  },
  watch: {
    num() {
      this.data.checklist = this.data.type == 5 ? [] : this.checkdefaultlist[this.data.type];
    }
  },
  mounted() {},
  // computed:{
  //   ClassName(){

  //   }
  // },
  components: {
    Upload,
    Linkdiv
  },

  methods: {
    changetype(a) {
      if (this.data.type == 0) {
        this.data.height = 187.5;
      } else if (this.data.type == 1) {
        this.data.height = 125;
      } else {
        this.data.height = 375;
      }
      this.data.checklist = this.data.type == 5 ? [] : this.checkdefaultlist[this.data.type];
    },
    // chooseLink() {

    // },
    selectlink() {
      let item = this.data.checklist[this.actvi_id].linkdata;
      this.$parent.showlinkselcet(item);
    },
    getAData(item) {
      // this.data.imglist[this.activid].linkdata=item
      this.data.checklist[this.actvi_id].linkdata = item;
    },
    changeitem(i) {
      this.actvi_id = i;
      this.img = this.data.checklist[i].img;
    },
    getimg(img) {
      const _this = this;
      if (this.actvi_id == 0 && (this.data.type == 0 || this.data.type == 1)) {
        getImageInfo(img, function(width, height) {
          let myheight = (375 / (Number(_this.data.type) + 2) / width) * height;
          _this.data.height = myheight;
        });
      }
      this.img = img;
      this.data.checklist[this.actvi_id].img = img;
    },
    getCalss(x, y) {
      let xlist = [];
      let ylist = [];

      if (this.nextitem && this.sitem) {
        let p = this.getnum();
        xlist = p.xlist;
        ylist = p.ylist;
        if (x >= xlist[0] && x <= xlist[1] && (y >= ylist[0]) & (y <= ylist[1])) {
          return 'selecting';
        } else {
          return '';
        }
      } else {
        return '';
      }
    },
    overitem(x, y) {
      // this.selectlist.push({
      //   x: x,
      //   y: y
      // });
      this.nextitem = {
        x: x,
        y: y
      };
    },
    clickitem(x, y) {
      if (this.sitem) {
        this.nextitem = {
          x: x,
          y: y
        };
        this.addlist();
      } else {
        this.sitem = {
          x: x,
          y: y
        };
      }
      // if (this.selectlist.length < 2) {
      //   this.selectlist.push({
      //     x: x,
      //     y: y
      //   });
      // } else {
      //   this.selectlist.forEach(c => {});
      // }
    },
    getnum() {
      let xlist = [];
      let ylist = [];
      xlist.push(this.sitem.x);
      xlist.push(this.nextitem.x);
      ylist.push(this.sitem.y);
      ylist.push(this.nextitem.y);
      ylist.sort((a, b) => {
        return a - b;
      });
      xlist.sort((a, b) => {
        return a - b;
      });
      let p = {
        ylist: ylist,
        xlist: xlist
      };
      return p;
    },
    addlist() {
      let p = this.getnum();
      let xlist = p.xlist;
      let ylist = p.ylist;
      let width = ((xlist[1] - xlist[0] + 1) / this.num) * 100;
      let height = ((ylist[1] - ylist[0] + 1) / this.num) * 100;

      let data = {
        width: width,
        height: height,
        x: (xlist[0] / this.num) * 100,
        y: (ylist[0] / this.num) * 100,
        img: '',
        linkdata: {
          link: '',
          name: '',
          id: '',
          type: ''
        }
      };
      this.sitem = null;
      this.nextitem = null;
      this.data.checklist.push(data);
      this.actvi_id = this.data.checklist.length - 1;
    }
  }
};
</script>

<style lang="scss" scoped>
.custom_box {
  margin-top: 10px;
  position: relative;
  width: 375px;
  height: 375px;
  display: flex;
  .cube-items {
    display: flex;
    flex-direction: column;
    flex: 1;
    .cube-item {
      display: flex;
      align-items: center;
      justify-content: center;
      border-left: 1px solid #e5e5e5;
      border-top: 1px solid #e5e5e5;
      flex: 1;
      box-sizing: border-box;
      background: #f4f6f8;
      &.selecting {
        background: #e8f7fd;
        border-color: #e5e5e5;
      }
    }
  }
  .cue-check-item {
    position: absolute;
    background: rgba(45, 140, 240, 0.2);
    border: 1px solid #e5e5e5;
    box-sizing: border-box;
    .item-box {
      height: 100%;
      width: 100%;
      background-color: #fff;
      display: flex;
      .cube-text {
        border-width: 1px 0 1px 1px;
        border-style: solid;
        border-color: #e9edef;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        cursor: pointer;
        box-sizing: border-box;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
        .text {
          font-size: 12px;
          line-height: 16px;
          text-align: center;
          color: #636669;
        }
      }
    }
    &.active {
      .cube-text {
        border: 1px solid #2d8cf0;
        .text {
          color: #2d8cf0;
        }
      }
    }

    .delitem {
      position: absolute;
      top: -12px;
      right: -12px;
      font-size: 20px;
      height: 20px;
      line-height: 20px;
      width: 20px;
      color: #636669;
      cursor: pointer;
      z-index: 9999;
    }
  }
}
.item {
  position: relative;
  width: 100%;
  display: flex;
  cursor: pointer;
  background: #f4f6f8;
  border-radius: 2px;
  box-sizing: border-box;
  border: 1px solid #e9edef;
  margin-bottom: 10px;
  padding: 10px 10px;

  &:hover {
    .icon-abl-close {
      display: block;
    }
  }

  .icon-abl-close {
    position: absolute;
    top: -6px;
    right: -9px;
    color: rgba(0, 0, 0, 0.3);
    display: none;
    font-size: 16px;
  }

  .dragbox {
    color: #909399;
    line-height: 60px;
    font-size: 16px;
    margin-right: 10px;
  }

  .img_box {
    width: 60px;
    height: 60px;
  }

  .item_select {
    flex: 1;
    margin-left: 10px;

    .select_link {
      background: #fff;

      .select_btn {
        padding: 4px 6px;
        color: #2d8cf0;
        box-sizing: border-box;
        border: 1px solid #fff;

        &:hover {
          border: 1px solid #2d8cf0;
        }
      }
    }

    .tip {
      display: block;
      font-family: PingFang SC;
      font-style: normal;
      font-weight: normal;
      font-size: 12px;
      line-height: 18px;
      color: #939799;
      /* margin-top: 8px; */
    }
  }
}
</style>
